package com.galangel.bookservice.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.galangel.bookservice.domain.pojo.BookInfo;
import com.galangel.bookservice.domain.pojo.BookTag;
import com.galangel.bookservice.domain.pojo.Tag;
import com.galangel.bookservice.domain.vo.BookInfoVo;
import com.galangel.bookservice.service.IBookInfoService;
import com.galangel.bookservice.service.IBookTagService;
import com.galangel.bookservice.service.ITagService;
import com.galangel.common.domain.R;
import com.galangel.common.utils.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 内容和目录信息 前端控制器
 * </p>
 *
 * @author galangel
 * @since 2025-03-15
 */
@Api(tags = "书目元数据接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/book-info")
public class BookInfoController {
    private final IBookInfoService bookInfoService;
    private final ITagService tagService;
    private final IBookTagService bookTagService;

    @ApiOperation(value = "新增内容")
    @PostMapping
    public R<?> addBookInfo(@RequestBody BookInfo bookInfo) {
        return R.ok(bookInfoService.save(bookInfo));
    }

    @ApiOperation(value = "删除内容")
    @DeleteMapping("/{id}")
    public R<?> deleteBookInfoById(@PathVariable Long id) {
        return R.ok(bookInfoService.removeById(id));
    }

    @ApiOperation(value = "根据id查询内容")
    @GetMapping("/{id}")
    public R<?> getBookInfoById(@PathVariable Long id) {
        return R.ok(bookInfoService.getById(id));
    }

    @ApiOperation(value = "查询所有内容")
    @GetMapping("/list")
    public R<?> listBookInfo() {
        List<BookInfoVo> bookInfoVo = new ArrayList<>();
        List<BookInfo>  bookInfolist = bookInfoService.list();
        bookInfolist.forEach(bookInfo -> {
            BookInfoVo bookInfoVo1 = BeanUtils.copyProperties(bookInfo, BookInfoVo.class);
            if (bookInfoVo1 != null) {
                bookInfoVo.add(bookInfoVo1);
            }
        });
//        bookTagService.list(
//                Wrappers.<BookTag>lambdaQuery().in(BookTag::getBookId, bookInfoVo.getBookId())
//        )
//        List<Tag> tags = tagService.list();
//        bookInfoVo.setTags(tags);
        return R.ok(bookInfoVo);
    }

    @ApiOperation(value = "更新内容")
    @PutMapping
    public R<?> updateBookInfo(@RequestBody BookInfo bookInfo) {
        return R.ok(bookInfoService.updateById(bookInfo));
    }
}
